<template>
  <div class="loginBox">
    <div class="account-wrap">
      <div>
        <div class="title">欢迎使用看板</div>
        <div class="sub-title">账号</div>
        <div class="input-wrap" :class="accountError ? 'input-wrap-error' : ''">
          <el-input v-model="account" placeholder="请输入账号"></el-input>
        </div>
        <div class="sub-title">密码</div>
        <div class="input-wrap" :class="pwdError ? 'input-wrap-error' : ''">
          <el-input
            v-model="pwd"
            placeholder="请输入密码"
            show-password
          ></el-input>
        </div>
        <el-button class="btn-commit" @click="handleLogin">登录</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import { accountLogin } from "@/api/login";

import store from "@/store";
export default {
  name: "login",
  props: {},
  components: {},
  data() {
    return {
      enterFunction: null,
      account: "",
      pwd: "",
      accountError: false,
      pwdError: false,
    };
  },
  methods: {
    handleEnter(event) {
      if (event.key === "Enter") {
        this.handleLogin();
        // 你可以在这里添加其他逻辑
      }
    },
    async handleLogin() {
      // 校验入参
      if (!this.account) {
        this.$message.warning("请输入账号！");
        return false;
      }
      if (!this.pwd) {
        this.$message.warning("请输入密码！");
        return false;
      }
      // 定义变量
      const params = {
        username: this.account,
        password: this.pwd,
      };
      let res;
      // 请求并处理异常
      try {
        res = await accountLogin(params);
      } catch (e) {
        this.$message.warning("网络异常，请稍后重试！");
        return false;
      }
      // 处理响应结果
      if (res && res.error_code === 0) {
        this.$message.success("登陆成功");
        localStorage.setItem(
          "loginInfo",
          JSON.stringify({
            account: this.account,
            pwd: this.pwd,
          })
        );
        console.log("debug00001", localStorage.getItem("loginInfo"));
        res?.data?.token && this.loginSuccess(res.data);
        // todo 跳转
      } else if (res && res.error_code === 1004) {
        // this.accountError = true
        this.$message.error("账号或密码错误!");
      } else if (res && res.error_code === 1000) {
        this.$message.error(
          res.msg?.password ? res.msg?.password[0] : res.msg?.username[0]
        );
      } else {
        this.$message.warning("网络异常，请稍后重试！");
      }
    },

    // 登陆完成
    async loginSuccess(data) {
      try {
        await store.dispatch("user/setTokenLogin", data);
        // await store.dispatch("user/getUserInfo");
        console.log("loginSuccess2", store.getters.uId);
        this.$router.push("/orgList");
      } catch (err) {
        console.log(err);
      }
    },
  },
  computed: {},
  watch: {
    account() {
      this.accountError = false;
      this.pwd = null;
      this.pwd = "";
    },
  },
};
</script>

<style lang='scss' scoped>
.loginBox {
  position: relative;
  min-height: 100%;
  width: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 0;
  background-image: url(../../assets/login.png);

  .account-wrap {
    width: 400px;
    height: 460px;
    background: #ffffffbf;
    border-radius: 24px;
    padding: 0px 48px 0 48px;
    // filter: blur(8px);
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    backdrop-filter: blur(8px);

    .title {
      font-size: 20px;
      color: #000000;
      text-align: center;
      line-height: 40px;
      margin-top: 32px;
      margin-bottom: 24px;
      font-weight: bold;
    }

    .sub-title {
      font-weight: 500;
      font-size: 14px;
      color: #1d1d1f;
      line-height: 20px;
    }

    .input-wrap-error ::v-deep {
      .el-input__inner {
        background: #ffcfcf !important;
        border: 1px solid #e30000 !important;
        color: #1d1d1f !important;
      }
    }

    .input-wrap ::v-deep {
      margin-top: 12px;
      margin-bottom: 20px;
      position: relative;

      .el-input__clear {
        font-size: 18px;
        margin-right: 12px;
      }

      .el-input__inner {
        background: #ffffffbf;

        width: 304px;
        height: 48px;
        line-height: 48px;

        border-radius: 24px;
        padding-left: 24px;
        padding-right: 46px;
        font-weight: 500;
        font-size: 16px;
      }

      .el-input__inner::-webkit-input-placeholder {
        /* WebKit browsers，webkit内核浏览器 */
        color: #00000059;
        font-size: 14px;
      }

      .el-input__inner:-moz-placeholder {
        /* Mozilla Firefox 4 to 18 */
        color: #00000059;
        font-size: 14px;
      }

      .el-input__inner::-moz-placeholder {
        /* Mozilla Firefox 19+ */
        color: #00000059;
        font-size: 14px;
      }

      .el-input__inner:-ms-input-placeholder {
        /* Internet Explorer 10+ */
        color: #00000059;
        font-size: 14px;
      }
    }

    .btn-commit {
      width: 304px;
      height: 48px;
      background: #1d1d1f;
      border-radius: 24px;
      font-size: 18px;
      color: #ffffff;
      margin-top: 36px;

      &:hover {
        background: #000 !important;
      }
    }
  }
}
</style>